package algorithm.leetcode;

import algorithm.leetcode.codeutils.ListNode;

/**
 * @program: util
 * @description: 两两交换链表中的节点
 * @author: zhouliangliang
 * @create: 2021-04-12 20:52
 **/
public class ExchangeNode {

    public ListNode<Integer> exchange(ListNode<Integer> head) {
        if(head == null || head.getNext() == null) {
            return head;
        }

        ListNode<Integer> second = head.getNext();
        ListNode<Integer> third = second.getNext();
        second.setNext(head);
        head.setNext(exchange(third));
        return second;
    }

    public static void main(String[] args) {
        ListNode<Integer> nodes = new ListNode<>(1, new ListNode<>(2, new ListNode<>(3, new ListNode<>(4, new ListNode<>(5, null)))));
        new ExchangeNode().exchange(nodes).toString();
    }
}
